import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import path from "path";

import Unocss from "unocss/vite";

import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import ElementPlus from "unplugin-element-plus/vite";

// https://vite.dev/config/
export default defineConfig({
  //配置@查找文件
  resolve: {
    alias: [
      {
        find: "@",
        replacement: path.resolve(__dirname, "src")
      }
    ]
  },

  plugins: [
    vue(),
    Unocss(),
    AutoImport({
      // 动态导入Vue,vue-router,pinia的相关api也导入进来 ，这样页面上就用再通过import vue from vue这种格式了
      imports: ["vue", "vue-router", "pinia"],

      // 去解析API，这个配置会自动生成auto-imports.d.ts文件
      resolvers: [ElementPlusResolver()],

      eslintrc: { enabled: false } //给eslint生产的配置，只需要一次
    }),
    Components({
      // 解析组件，
      resolvers: [ElementPlusResolver()],

      //让用户在以下目录自定义的组件可以在要使用的页面上可以自动加载，会自动生成components.d.ts这个文件
      // 所有的组件可以自动加载
      dirs: [
        "src/components",
        "src/layout/components",
        "src/views/**/components"
      ]
    }),

    //动态导入ElmentUI的样式
    ElementPlus({})
  ],
  server: {
    proxy: {
      "/dev-api": {
        target: "http://localhost:3000",
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/dev-api/, "/api")
      }
    }
  }
});
